//
//  VisitorLoginView.swift
//  WeiBo001
//
//  Created by Zhanghong on 15/6/27.
//  Copyright © 2015年 HeiMa. All rights reserved.
//

import UIKit
import FFAutoLayout

protocol VisitorLoginViewDelegate : NSObjectProtocol {
    func visitorRegisterButtonClick()
    func visitorLoginButtonClick()
}

class VisitorLoginView: UIView {
    weak var delegate : VisitorLoginViewDelegate?
    
    func setupViewInfo(iconName: String, message:String, isHome: Bool){
        if isHome {
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        }else {
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            sendSubviewToBack(maskIconView)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.redColor()
        
        setupUI()
//        startAnimation()
    }
    
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    func login(){
        delegate?.visitorLoginButtonClick()
    }
    
    func register(){
        delegate?.visitorRegisterButtonClick()
    }
    
    func setupUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)

        
        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -40))
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: iconView, size: nil)
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: CGSize(width: 240, height: 40), offset: CGPoint(x: 0, y: 20))

        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        
        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        
        maskIconView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom: 160, right: 0))
//        
        
        
//        // 遮罩视图的布局
//        maskIconView.translatesAutoresizingMaskIntoConstraints = false
//        // 水平方向
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : maskIconView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]-160-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : maskIconView]))
        
        // 背景颜色 237.0 / 255.0 `237` 这个数值是美工会告诉我们
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 会旋转的背景图标
    lazy var iconView : UIImageView = {
       let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    lazy var maskIconView : UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    
    /// 小房子图标
    lazy var homeIconView : UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
    /// 消息文本
    lazy var messageLabel : UILabel = {
        let label = UILabel(frame: CGRectMake(0, 0, 240, 40))
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.preferredMaxLayoutWidth = 224
        // 最多两行，超出之后...
        label.numberOfLines = 2
        
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    lazy var registerButton : UIButton = {
       let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    /// 登录按钮
    lazy var loginButton : UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()
    
    
}
